package function;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * create by: 蔡博
 * description: TODO
 * create time: 2022/11/18 11:14
 * 模拟栈实现递归
  * @Param: null
 * @return
 */
public class StackImitateRecursion {

}

class jiecheng{
    public static void main(String[] args) {
        jiecheng t = new jiecheng();
        System.out.println(t.r(19));
        System.out.println(t.s(19));
    }
    int r(int n){
        if(n==1)
            return 1;
        return n*r(n-1);
    }

    int s(int n){
        int res = 1;
        class data{
            private int n;
            int v;

            public data(int n, int v) {
                this.n = n;
                this.v = v;
            }
        }
        Deque<data> stack = new ArrayDeque();
        stack.addFirst(new data(n,5));
        while(!stack.isEmpty()){
            data data = stack.removeFirst();
            int num = data.n;
            int v = data.v;
            if(v==5){
                if(num==1)
                    continue;
                else {
                    stack.addFirst(new data(num,1));
                    stack.addFirst(new data(num-1,5));
                }
            }else{
                res*=num; //res是要传递给下一层的值
            }

        }
        return res;
    }
}